package com.dmc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.dmc.dao.PayrollDao;
import com.dmc.dto.PayrollDto;
import com.neusoft.chenanqi.dao.EmpSalaryDao;
import com.zero.basic.exception.SmsDaoException;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.model.Pager;
import com.zero.mms.model.EmpSalary;
import com.zero.mms.model.SalaryItem;

@Service("payrollService")
public class PayrollService {
	@Inject
	private PayrollDao payrollDao;
	@Inject
	private EmpSalaryDao empSalaryDao;
	

	// 获取工资项目名称列表
	public List<String> getPayrollItemList() throws SmsDaoException {
		List<SalaryItem> salaryItems = payrollDao.listPayrollItems();
		List<String> salaryItemNames = new ArrayList<String>();
		for (SalaryItem salaryItem : salaryItems) {
			salaryItemNames.add(salaryItem.getSalaryItemName());
		}
		return salaryItemNames;
	}
	
	//获取工资报表所有显示的表头信息
	public List<String> getPayrollItems() throws SmsDaoException {
		List<String> salaryItemNames = new ArrayList<String>();
		salaryItemNames.add("部门编号");
		salaryItemNames.add("部门名称");
		salaryItemNames.add("员工编号");
		salaryItemNames.add("员工名称");
		
		salaryItemNames.addAll(this.getPayrollItemList());
		
		salaryItemNames.add("发放月");
		return salaryItemNames;
	}
	
		
	//获取项目数据列表
	public List<Map<String, String>> getPayrollRowData(String depId, String empNo, 
			String fromDate, String toDate) throws SmsDaoException, SmsRuntimeException {
		try {
			if (StringUtils.isBlank(empNo)) {
				empNo = "";
			}
			if (StringUtils.isBlank(depId)) {
				depId = "";
			}
			List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
			
			// 获得工资记录的人员月份信息
			List<PayrollDto> payrollDtoList = payrollDao.listPayroll(depId, empNo, fromDate, toDate);
			if (payrollDtoList.isEmpty() || payrollDtoList==null) {
				return dataList;
			}
			
			// 获取工资项目名称列表
			List<String> salaryItemNames = this.getPayrollItemList();
			
			
			// 获得每条工资记录的所有固定工资项目值
			
			for (PayrollDto payrollDto : payrollDtoList) {
				int i = 0;
				Map<String, String> sal = new HashMap<String, String>();
				sal.put("部门编号", payrollDto.getDepNo().toString());
				sal.put("部门名称", payrollDto.getDepName());
				sal.put("员工编号", payrollDto.getEmpNo().toString());
				sal.put("员工名称", payrollDto.getEmpName());
//				
				for (String itemName : salaryItemNames) {
					EmpSalary empSalary = empSalaryDao.getEmpSalByEmpNoMonthAndItemName(payrollDto.getEmpNo().toString(),
							payrollDto.getEmpSalaryDate(), itemName);
					if (empSalary == null || empSalary.getEmpSalaryNum() == null || "".equals(empSalary.getEmpSalaryNum())) {					
						sal.put(itemName, "0");
					} else {
						sal.put(itemName, empSalary.getEmpSalaryNum());	
					}
				}	

				sal.put("发放月", payrollDto.getEmpSalaryDate());
				dataList.add(sal);
			}
			
			return dataList;
			} catch (NumberFormatException e)  {
				e.printStackTrace();
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.INVALID_AUGUMENT, "工资范围输入错误");
			} catch (Exception e) {
				e.printStackTrace();
				throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
						SmsErrorConstant.WEB_APPLICATION, "服务器错误");
			}
	}
}
